Page({
  data: {
    category: '',
    foodList: [],
    searchQuery: '',
  },

  // 页面加载时初始化分类和食物列表
  onLoad(options) {
    const category = options.category;
    this.setData({ category });
    this.updateFoodList(category);
  },

  // 页面显示时更新导航栏标题
  onShow() {
    wx.setNavigationBarTitle({
      title: this.data.category,
    });
  },

  // 处理输入框内容变化
  onInput(event) {
    const searchQuery = event.detail.value;
    this.setData({ searchQuery });
    this.filterFoodList(searchQuery);
  },

  // 根据分类获取食物列表
  getFoodListByCategory(category) {
    const foodData = {
      '谷物': [
        { id: 1, name: '小米', imgUrl: '/images/food-images/millet.jpg', benefits: '富含膳食纤维，有助于消化', favorite: false },
        { id: 2, name: '大米', imgUrl: '/images/food-images/rice.jpg', benefits: '提供能量，是主食之一', favorite: false }
      ],
      '应季水果': [
        { id: 3, name: '苹果', imgUrl: '/images/food-images/apple.jpg', benefits: '含有丰富的维生素C', favorite: false },
        { id: 4, name: '香蕉', imgUrl: '/images/food-images/banana.jpg', benefits: '富含钾，有助于心脏健康', favorite: false }
      ],
      '绿色蔬菜': [
        { id: 5, name: '菠菜', imgUrl: '/images/food-images/spinach.jpg', benefits: '含有丰富的铁质和维生素', favorite: false },
        { id: 6, name: '西兰花', imgUrl: '/images/food-images/broccoli.jpg', benefits: '含有抗氧化剂，有助于防癌', favorite: false }
      ],
      '豆类': [
        { id: 7, name: '黄豆', imgUrl: '/images/food-images/soybean.jpg', benefits: '富含蛋白质和纤维', favorite: false },
        { id: 8, name: '红豆', imgUrl: '/images/food-images/redbean.jpg', benefits: '有助于降低胆固醇', favorite: false }
      ],
      '坚果': [
        { id: 9, name: '杏仁', imgUrl: '/images/food-images/almond.jpg', benefits: '富含维生素E，有助于皮肤健康', favorite: false },
        { id: 10, name: '核桃', imgUrl: '/images/food-images/walnut.jpg', benefits: '富含Omega-3脂肪酸，有助于大脑健康', favorite: false }
      ],
      '肉类': [
        { id: 11, name: '鸡肉', imgUrl: '/images/food-images/chicken.jpg', benefits: '富含蛋白质，有助于肌肉生长', favorite: false },
        { id: 12, name: '牛肉', imgUrl: '/images/food-images/beef.jpg', benefits: '富含铁质，有助于补血', favorite: false }
      ]
    };
    return foodData[category] || [];
  },
  // 更新食物列表
  updateFoodList(category) {
    const foodList = this.getFoodListByCategory(category);
    this.setData({ foodList });
  },
  // 过滤食物列表
  filterFoodList(searchQuery) {
    const category = this.data.category;
    let foodList = this.getFoodListByCategory(category);
    if (searchQuery) {
      foodList = foodList.filter(item => item.name.toLowerCase().includes(searchQuery.toLowerCase()));
    }
    this.setData({ foodList });
  },

  // 切换收藏状态
  toggleFavorite(event) {
    const id = event.currentTarget.dataset.id;
    const foodList = this.data.foodList.map(item => {
      if (item.id === id) {
        return { ...item, favorite: !item.favorite };
      }
      return item;
    });
    this.setData({ foodList });
    // 获取并更新本地存储的收藏列表
    let storedFavoriteFoodList = wx.getStorageSync('favoriteFoodList') || [];
    const updatedFavoriteFoodList = foodList.filter(item => item.favorite);
    const combinedFavoriteFoodList = [...new Set([...storedFavoriteFoodList, ...updatedFavoriteFoodList])];
    wx.setStorageSync('favoriteFoodList', combinedFavoriteFoodList);
  },
});
